package com.readboy.changefilename;


import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: jayclin
 * @Date: 2020/09/30/11:53
 * @Description:
 */
public class FileNameChange {
    private static final String APP_ID = "20200930000577269";
    private static final String SECURITY_KEY = "gUNFkxAZ4aBAPS0GBgU1";

    /**
     * 从json字符串中提取英文翻译
     * @param jsonData
     * @return
     */
    private static String parse(String jsonData) {
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray results = jsonObject.getJSONArray("trans_result");
            JSONObject result = results.getJSONObject(0);
            return (String) result.get("dst");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 将path路径下所有的文件名改为英文
     * @param path
     * @throws UnsupportedEncodingException
     * @throws InterruptedException
     */
    public static void changeFileName(String path) throws UnsupportedEncodingException, InterruptedException {
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        File file = new File(path);
        File[] list = file.listFiles();

        if (file.exists() && file.isDirectory()) {
            for (int i = 0; i < list.length; i++) {
                String name = list[i].getName();
                int index = name.indexOf(".");
                String name2 = name.substring(0, index);//文件名前缀
                int index2 = name.lastIndexOf(".");
                String name3 = name.substring(index2);
                Thread.sleep(1000);//百度API的免费版本1秒只能有一个接入
                String result = api.getTransResult(name2, "auto", "en");
                String enString = parse(result);
                enString=enString.replace("-", "");
                String newName = enString.replace(' ', '_') + name3;
                //重命名
                File dest = new File(path + "/" + newName.toLowerCase());
                list[i].renameTo(dest);
                System.out.println(dest.getName());
            }
        }
    }

    /**
     * 改变android的Drawable文件夹的不同dp的所有文件名，只需将该drawable文件路径传入
     * @param path
     */
    public static void changeDrawableFile(String path){
        File file=new File(path);
        File[] list=file.listFiles();
        if (file.exists()&&file.isDirectory()){
            for (int i=0;i<list.length;i++){
                String name=list[i].getName();
                String newPath=path+"/"+name;
                try {

                    changeFileName(newPath);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        changeDrawableFile("/Users/mac/Downloads/英语全解切图/课后练习");
    }
}





